<template>
  <div>
    <el-drawer v-model="isShowComment" :direction="'ltr'" size="36%">
      <template #title>
        <header class="header">
          <h2 v-if="commentsType == 0">{{ audioMes.name }}</h2>
          <h2 v-if="commentsType == 3">{{ albumName }}</h2>
        </header>
      </template>

      <template #default>
        <div class="commentDiv">
          <p v-show="!tableData.length">现在还没有热评哦~~~</p>
          <div v-for="item in tableData" class="minDiv">
            <el-card class="box-card">
              <template #header>
                <div class="card-header">
                  <img :src="item.avatarUrl" alt="" class="avatar" />
                  <span>
                    <p>{{ item.nickname }}</p>
                  </span>
                </div>
              </template>
              <template #default>
                <p>{{ item.content }}</p>
                <div class="card-footer">
                  <p>{{ item.timeStr }}</p>
                  <p>
                    <el-icon class="pointLike"><Pointer /></el-icon> {{ item.likedCount }}
                  </p>
                </div>
              </template>
            </el-card>
          </div>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script>
import { defineComponent, ref, reactive, toRefs, watch } from "vue";
import { storeToRefs } from "pinia";
import { getComment } from "@/service/comment";
import { storage } from "@/stores/storage";
export default defineComponent({
  setup() {
    let store = storage();
    let tableData = ref([]);
    const { isShowComment, audioMes, commentId, albumName, commentsType } = storeToRefs(
      store
    );
    function getCommentFun() {
      tableData.value = [];
      getComment(commentId.value, commentsType.value).then((res) => {
        let data = res.data.hotComments;
        for (let i = 0; i < data.length; i++) {
          tableData.value[i] = {};
          tableData.value[i].content = data[i].content;
          tableData.value[i].timeStr = data[i].timeStr;
          tableData.value[i].userId = data[i].user.userId;
          tableData.value[i].avatarUrl = data[i].user.avatarUrl;
          tableData.value[i].nickname = data[i].user.nickname;
          tableData.value[i].commentId = data[i].commentId;
          tableData.value[i].likedCount = data[i].likedCount;
        }
      });
    }
    watch(
      () => isShowComment.value,
      (v1) => {
        if (v1 == true) {
          getCommentFun();
        }
      },
      { deep: true, immediate: true }
    );
    return {
      isShowComment,
      getCommentFun,
      audioMes,
      albumName,
      tableData,
      commentsType,
      // toUserPage,
    };
  },
});
</script>

<style scoped>
.commentDiv {
  background-color: white;
  width: 90%;
  height: 500px;
  overflow: scroll;
  scrollbar-width: none;
}
.header {
  font-size: large;
}
.minDiv {
  background-color: rosybrown;
  width: 95%;
  margin: 5px 10px;
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin: 0 10px;
}
.card-header {
  display: flex;
  justify-content: start;
  align-items: center;
}
.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.pointLike {
  font-size: 20px;
  position: relative;
  top: 5px;
}
</style>
